<?php
class work extends head { 
 function work() {
  $this->table = "work";
  $this->getmetadata();
  $this->maintainhist = false;
  $this->header[2] = "";
  $_SESSION[strtoupper($this->table)] = $this;
  $_SESSION["work"] =$this;
 }
 
 function applyrefs() {
  parent::applyrefs();
  $this->refs[5] = freadfield("employee",$this->header[5],"name");
  $this->refs[6] = freadfield("process",$this->header[6],"name");
  $this->refs[7] = freadfield("pgroup",$this->header[7],"name");
  $this->refs[8] = freadfield("jobs",$this->header[8],"name");  
 }
 
  function dateselectlist($fromday,$today,$name,$selected) {
   // both days are offsets from today, both can be negative.
      
   $h = "<select name=$name>";
   for ($i=$fromday;$i<=$today;$i++) {
    $jd = isotojd()+$i;
    ($selected == dtoiso(jdtoout($jd))) ? $s = "selected" :$s = "";
    $h .= "<option $s value='".dtoiso(jdtoout($jd))."'>".jdtoout($jd)."</option>";
   }
   $h .= "</select>";
   
   return $h;
 }

 
 function createblank() {
  $this->header = array_fill(0,$this->atts,"");
  $this->header[2] = "0";
  $this->valids = array_fill(0,$this->atts,"");
  $this->lists = array_fill(0,$this->atts,"");
  $this->refs = array_fill(0,$this->atts,"");  
  $this->header[18] = dtoiso("");
  $this->header[11] = "0000-00-00 00:00:00";
  $this->header[12] = "0000-00-00 00:00:00";
  $this->header[13] = "";
  $user = authenticate(0);
 }
 
  function selectlist($condition) {
  // Last search cached for speed.
  if ($condition == $this->lastsearch) {
    return $this->lastsearchresults;
  } else {
	  $stmt = "select itemid,name,jobid,processid,starttime,endtime,truncATE((unix_timestamp(endtime)-unix_timestamp(starttime))/3600,0) from ".$this->table;
	  $words = explode(" ",$condition);
	  if ($condition !=="" and $condition !=="ALL") {
	   if (sizeof($words > 0)) {
	    $stmt .= " where CONCAT(name,processid,jobid,starttime) like '%$words[0]%'";
	    for ($i=1;$i<sizeof($words);$i++) {
	     $stmt .= " and CONCAT(name,processid,jobid,starttime) like '%$words[$i]%'";
	    }
	   } else {
	    $stmt .= " where CONCAT(name,processid,jobid,starttime) like '%$condition%'";
	   }
	  }
	  $stmt .= " order by editdate desc limit 1000";
	  $ans = sqlreadarray($stmt);
	  $this->lastsearchresults = $ans;
	  $this->lastsearch = $condition;
    return $ans;
  }
 }
 
 function applyfromrequest() {
  // Mapped requests F1-> field 1 etc, L1->Line 1 etc
  
  $this->valid = true;
  $this->valids = array_fill(0,$this->atts,"");  
  $this->lists = array_fill(0,$this->atts,"");


  $i = 5; $t = "employee";
	if ($f=@$_REQUEST["f$i"]) {
   // Employee object id
   if ($f !== $this->header[$i]) {
    $desc = freaditem($t,$f);
    if ($desc == "") {
     $this->valids[$i] = "Need valid $t code";
     $this->lists[$i] = $this->selectlisth($t,$f,"f$i");
    } else {
     $this->header[$i] = $f;
     $this->refs[$i] = $desc[1];
     $this->header[1] = $desc[1];
    }
   }
  }

  $i = 6; $t = "process";
	if ($f=@$_REQUEST["f$i"]) {
   // Process object id
   if ($f !== $this->header[$i]) {
    $desc = freaditem($t,$f);
    if ($desc == "") {
     $this->valids[$i] = "Need valid $t code";
     $this->lists[$i] = $this->selectlisth($t,$f,"f$i");
    } else {
     $this->header[$i] = $f;
     $this->refs[$i] = $desc[1];
     $this->lists[$i] = "";
    }
   }
  }

  $i = 7; $t = "pgroup";
	if ($f=@$_REQUEST["f$i"]) {
   // Product Group object id
   if ($f !== $this->header[$i]) {
    $desc = freaditem($t,$f);
    if ($desc == "") {
     $this->valids[$i] = "Need valid $t code";
     $this->lists[$i] = $this->selectlisth($t,$f,"f$i");
    } else {
     $this->header[$i] = $f;
     $this->refs[$i] = $desc[1];
     $this->lists[$i] = "";
    }
   }
  }

  $i = 8; $t = "jobs";
	if ($f=@$_REQUEST["f$i"]) {
   // Jobs object id
   if ($f !== $this->header[$i]) {
    $desc = freaditem($t,$f);
    if ($desc == "") {
     $this->valids[$i] = "Need valid job code";
     $this->lists[$i] = $this->selectlisth($t,$f,"f$i");
    } else {
     $this->header[$i] = $f;
     $this->refs[$i] = $desc[1];
     $this->lists[$i] = "";
    }
   }
  }
   
  $i = 9;
	if ($f=@$_REQUEST["f$i"]) {
	 if (preg_match("/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/",$f)) {
 	  $this->header[$i] = $f;
	 } else {
	  $this->valids[$i] = "Wrong format use YYYY-MM-DD HH:MM:SS eg 2009-12-31 15:15:00";
	 }
  }
	  
  $i = 10;
	if ($f=@$_REQUEST["f$i"]) {
	 if (preg_match("/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/",$f)) {
 	  $this->header[$i] = $f;
	 } else {
	  $this->valids[$i] = "Wrong format use YYYY-MM-DD HH:MM:SS eg 2009-12-31 15:15:00";
	 }
  }

  $i = 11;
	if ($f=@$_REQUEST["f$i"]) {
	 if (preg_match("/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/",$f)) {
 	  $this->header[$i] = $f;
	 } else {
	  $this->valids[$i] = "Wrong format use YYYY-MM-DD HH:MM:SS eg 2009-12-31 15:15:00";
	 }
  }
  
  $i = 12;
	if ($f=@$_REQUEST["f$i"]) {
	 if (preg_match("/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/",$f)) {
 	  $this->header[$i] = $f;
	 } else {
	  $this->valids[$i] = "Wrong format use YYYY-MM-DD HH:MM:SS eg 2009-12-31 15:15:00";
	 }
  }
  

  $i = 13;
  if ($f=@$_REQUEST["f$i"]) {
   // name. Allowable characters A-Za-z-space apostrophe
   if(preg_match('/^[0-9a-z A-Z\'\.-]{2,100}$/',$f)) {
    $this->header[$i] = $f; 
   } else {
    $this->valids[$i] = "Please enter a valid note made up of letters and numbers";
   }
  }
  

  if ($this->header[5] == "") {
   $this->valids[5] = "Cannot leave employee blank";
  }
  if ($this->header[6] == "") {
   $this->valids[6] = "Cannot leave process blank";
  }
  if (strtotime($this->header[10]) < strtotime($this->header[9])) {
   $this->valids[9] = "Start time must be before end time";
  }

 }
 
 function newcounter() {
  return "0";
 }
  
 function writedb() {
//  if ($this->header[9] == $this->header[10] && $this->header[11] == $this->header[12]) {
//   $ans = parent::deletedb();
//  } else {
   $ans = parent::writedb();
//  }
  unset($_SESSION[$this->table]);

  return $ans;
 }

 function showform() {
  $focalpoint = "f5";
  echo "<head>";
  echo "<script src='datechooser.js' type='text/javascript'></script>";
  echo SCRIPTS ;
  //
  echo "</head>";
	echo "<body onload='document.form.".$focalpoint.".focus()'>";
	echo "<h1>Manage Work Items</h1>";
  echo "<form name=form method=POST>";
  echo "<input type=hidden name=res value=''>";
  echo "<table>";
  echo $this->htmlrow(5,"Employee");
  echo $this->htmlrow(6,"Process");
  echo $this->htmlrow(7,"Product Group");
  echo $this->htmlrow(8,"Job");
  echo $this->htmlrow(9,"Start Time");
  echo $this->htmlrow(10,"End Time");
  echo $this->htmlrow(11,"Schedule Start Time");
  echo $this->htmlrow(12,"Schedule End Time");
  echo $this->htmlrow(13,"Notes");	
  echo "</table>";
  echo "<input class=bt accesskey=A type=submit name=sub value=Apply>";
  echo "<input class=bt accesskey=S type=submit name=sub value=Save>";
  echo "<button class=bt onclick=rest()><span>Restart</span></button>"; 

  echo "</form>"; 
  // set up help
  echo($this->registerhelp());
 }


 function showall($showfrom,$showto) {
  $sql = "select * from jobs";
  $jobs = sqlreadkeyarray($sql);

  $boxwidth= 210 ; $rowheight = 20; $leftpos = 210; $toppos = 100;
  $n = 0;
  $img = "";
  $jobid = @$_REQUEST["jobid"];  
  if ($showto == "NULL") {$showto = date("Y-m-d",time());}
  if ($showfrom == "NULL") {$showfrom = date("Y-m-d");}
  $copyfrom = @$_REQUEST["copyfrom"];
  $copyto = @$_REQUEST["copyto"];

  $sql = "select * from employee ";
  $sql .= "where (finished > '$showfrom' or finished = 0) ";
  $sql .= "and department not in ('Admin','Purchasing')";
  $sql .= " order by department,name ";
  $list = sqlreadkeyarray($sql);
  
  $startpos = strtotime($showfrom);
  $endpos = strtotime($showto)+86400;
  $days = ($endpos-$startpos)/86400;
  $daywidth = @(600/$days);
	
  // Names as buttons
	foreach ($list as $key=>$id) {
	 $n = $n + 1;
   $img .= "<button onclick='document.form.empid.value=\"$id[0]\"; document.form.submit();' ";
	 $img .= "style='text-align:left;padding:0px 0px 0px 0px;border:solid;border-width:1px;";
	 $img .= "background-color:#D2E1F6; position:absolute;left:10px;top:";
	 $img .= ($n*$rowheight+$toppos)."px; width:";
	 $img .= "200px; height:";
	 $img .= $rowheight."px;' >".ucwords(strtolower($id[1]))."</button>\n";   
   $list[$key][2] = $n;
   // Day rectangles
   for ($day=0;$day<$days;$day+=1) {
    if ($n ==1 ) {
     if (date('D',$startpos+86400*$day) == 'Sun' || date('D',$startpos+86400*$day) == 'Sat') {
      $col = "#12E1F6";
     } else {
      $col = "#D2E1F6";
     }
     $d = date('d-m',$startpos+86400*$day);
     $f = max(7,min(14,26-$days));
     $jd = date('Y-m-d',$startpos+86400*$day);
     $img .= "<button ";
		 $img .= "style='font-color:#ffffff;font-size:{$f}px;text-align:center;padding:0px 0px 0px 0px;border:solid;border-width:1px;";
  	 $img .= "background-color:$col; position:absolute;left:";
 		 $img .= ($leftpos+$day*$daywidth)."px;top:";
	   $img .= $toppos."px; width:";
	   $img .= $daywidth."px; height:";
	   $img .= $rowheight."px;' >$d</button>\n";   
    }
    $img .= "<button onclick='' "; 
 	  $img .= "style='text-align:left;padding:0px 0px 0px 0px;border:solid;border-width:1px;";
	  $img .= "background-color:#e1f1F6; position:absolute;left:";
		$img .= ($leftpos+$day*$daywidth)."px;top:";
	  $img .= ($rowheight*$n+$toppos)."px; width:";
	  $img .= $daywidth."px; height:";
	  $img .= $rowheight."px;' ></button>\n";   
   } 
  }
  
  // Attendances as buttons
  $sql = "select empid,starttime,endtime,itemid,processid,jobid,pgroupid,starttime,endtime from work";  
  $sql .= " where starttime >= '$showfrom' and endtime <= '$showto 23:59:59' ";
  if ($jobid != "[]" && $jobid != NULL) {
   $sql .= " and jobid = '$jobid' or processid = 'ATT'";
  }
  $list2 = sqlreadarray($sql);
  
 if ($list2 != "") {
   foreach ((array)$list2 as $item) {
   $empid = $item[0];
   if (isset($list[$empid])) {
    $pos = $list[$empid][2];
    $frmsec = strtotime($item[1])-$startpos;
    $tosec = strtotime($item[2])-$startpos;
    $accpos = $frmsec*$daywidth/86400;
    $accwidth = ($tosec-$frmsec)*$daywidth/86400;
    if ($accpos >= 0 and $accpos < 800) {
     $downpos = $pos*$rowheight+$toppos;
		 $heightpos = $rowheight-4;
     $accpos = $accpos + $leftpos;
     $t = split(" ",$item[1]) ; $t = substr($t[1],0,5);
     $u = split(" ",$item[2]) ; $u = substr($u[1],0,5);
     $t = "$t to $u";
     if ($item[4] == 'ATT') {
      $img .= "<button onclick='document.form.itemid.value=$item[3];document.form.submit();' style='padding:0px 0px 0px 0px;border:solid;border-width:1px;background-color:#D2E1F6; position:absolute;left:{$accpos}px;top:{$downpos}px; width:{$accwidth}px; height:".($rowheight/2)."px;' title='$t'></a>\n";
     } else {
      $t = "$item[4] / $item[5] / $item[6] ".$t;
      $col = $jobs[$item[5]][9];
      if ($item[7] != "0000-00-00 00:00:00") {
       $img .= "<button onclick='document.form.itemid.value=$item[3];document.form.submit();' style='padding:0px 0px 0px 0px;border:solid;border-width:1px;background-color:#$col; position:absolute;left:{$accpos}px;top:".($downpos+$rowheight/2)."px; width:{$accwidth}px; height:".($rowheight/2)."px;'  title='$t'></a>\n";
      } else {
       $img .= "<button onclick='document.form.itemid.value=$item[3];document.form.submit();' style='padding:0px 0px 0px 0px;border:solid;border-width:1px;background-color:#$col; position:absolute;left:{$accpos}px;top:".($downpos+$rowheight/2)."px; width:{$accwidth}px; height:".($rowheight/2)."px;' title='$t'></a>\n";
			}      
     }
    }
   }
  }
  }
  
 

  $focalpoint = "f5";
  echo "<head>";
  echo SCRIPTS ;
  //
  echo "</head>";
	echo "<body onload='document.form.showfrom.focus()'>";
	echo "<form name=form>";
  echo "<input type=hidden name=res value=''>";
	echo "<input type=hidden name=itemid value=-1>";
	echo "<input type=hidden name=empid value=-1>";
  echo "<table>";
  echo "<tr><td>Show From</td>";
  echo "<td>";
	echo $this->dateselectlist(-15,45,'showfrom',$showfrom); 
  echo "</td>";
	echo "<td>Show To</td>";
  echo "<td>";
	echo $this->dateselectlist(-15,45,'showto',$showto); 
  echo "</td>";
  echo "<td>";
  echo "<input class=bt accesskey=A type=submit name=sub value=Apply>";
  echo "</td>";
  echo "</tr></table>";
  // set up help
  echo($this->registerhelp());
	echo "</form>";
	
	echo $img;
 }
 
}

class attendance extends work {

 function attendance() {
  $this->table = "work";
  $this->getmetadata();
  $this->maintainhist = false;
  $this->header[2] = "";
  $_SESSION["attendance"] =$this;
 }
 
 function confirmfiled($itemid=null,$showfrom,$showto) {
  echo "<br><br>";
  echo "<body onload='document.form.sub.focus()'>";
  echo "<h1>$itemid Filed as requested</h1>";
  echo "<form>";
  echo "<input type=hidden name=showfrom value='$showfrom'>";
  echo "<input type=hidden name=showto value='$showto'>";
  echo "<input class=bt accesskey=C type=submit name=sub value='Continue'>";
  echo "</form>";
 }

 
 function copyday($copyfrom,$copyto) {
/*
  // Copies attendance records only.
  // Clears down any scheduled attendance records. Deletes them if there is nothing else
  // worthwhile in the record
  $sql = "update work set ";
  $sql .="schedulestarttime = 0, scheduleendtime = 0 ";
  $sql .="where processid = 'ATT' ";
  $sql .="and starttime = 0 ";
  $sql .=" and date(schedulestarttime) = '$copyto'";
  sqlexec($sql);
	$sql = "insert into work";
	$sql .=" (select 0,name,1,now(),'if',empid,'ATT','','',0,0,";
	$sql .=" concat('$copyto ',time(schedulestarttime)),";
	$sql .=" concat('$copyto ',time(scheduleendtime)),0";
	$sql .=" from work";
	$sql .=" where processid = 'ATT'";
	$sql .=" and date(schedulestarttime) = '$copyfrom'";
	$sql .=" and starttime = 0) ";
  sqlexec($sql);
*/
 }

 function showattform() {
  $focalpoint = "f5";
  echo "<head>";
  echo "<script src='datechooser.js' type='text/javascript'></script>";
  echo SCRIPTS ;
  //
  echo "</head>";
	echo "<body onload='document.form.".$focalpoint.".focus()'>";
	echo "<h1>Attendance Items</h1>";
  echo "<form name=form method=POST>";
  echo "<input type=hidden name=res value=''>";
  echo "<input type=hidden name=f6 value=ATT>";
  echo "<table>";
  echo $this->htmlrow(5,"Employee");
  echo $this->htmlrow(11,"Schedule Start Time");
  echo $this->htmlrow(12,"Schedule End Time");
  echo "</table>";
  echo "<input class=bt accesskey=A type=submit name=sub value=Apply>";
  echo "<input class=bt accesskey=S type=submit name=sub value=Save>";
  echo "<button class=bt onclick=rest()><span>Restart</span></button>"; 

  echo "</form>"; 
  // set up help
  echo($this->registerhelp());
 }
}
?>